package com.example.springback.common;

import org.docx4j.Docx4J;
import org.docx4j.convert.out.html.AbstractHtmlExporter;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;

public class WordToHtmlConverter {
    public static String convert(MultipartFile file) throws Exception {
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(file.getInputStream());
        // 设置图片路径
        String imageDirPath = "your-static-folder/images/";
        File imageDir = new File(imageDirPath);
        imageDir.mkdirs();
        AbstractHtmlExporter.HtmlSettings htmlSettings = new AbstractHtmlExporter.HtmlSettings();
        htmlSettings.setImageDirPath(imageDirPath);
        htmlSettings.setImageTargetUri("/static/images/");
        OutputStream out = new ByteArrayOutputStream();
        Docx4J.toHTML(htmlSettings, out, Docx4J.FLAG_EXPORT_PREFER_XSL);
        return out.toString();
    }
}
